$(function () {
    $('#register-btn').on('click', function () {
        var arr = $('#form').serializeArray()
        var obj = {};
        arr.forEach(element => {
            obj[element.name] = element.value.trim();
        });
        if (!obj.username) {
            mui.toast("请输入用户名");
            $('[name=username]').val('')
            return
        }
        if (obj.mobile.length < 11) {
            mui.toast("请输入合法的手机号");
            $('[name=mobile]').val('')
            return
        }
        if (!obj.password || !obj.againPass) {
            mui.toast("请输入密码");
            $('[name=password]').val('')
            return
        }
        if (obj.password != obj.againPass) {
            mui.toast("两次输入的密码不一样");
            $('[name=againPass]').val('')
            return;
        }
        if (!obj.vCode) {
            mui.toast("请输入验证码");
            $('[name=vCode]').val('')
            return
        }
        $.ajax({
            type: "POST",
            url: "/user/register",
            data: {
                username: obj.username,
                password: obj.password,
                mobile: obj.mobile,
                vCode: obj.vCode
            },
            success: function (res) {
                console.log(res);
                console.log(res.error);
                if (res.error) {
                    alert(res.message)
                } else {
                    $('#form')[0].reset()
                    alert('注册成功')
                    location.href = 'login.html'
                }
            }
        });
    })
    //获取验证码
    $('#getCode').on('click', function () {
        $.ajax({
            type: "get",
            url: "/user/vCode",
            success: function (res) {
                console.log(res);
                $('#getCode').text(res.vCode)
            }
        });
    })
    //返回按钮
    $('.mui-action-back').on('click', function () {
        window.history.back();
    })
})